package com.aia.web1.web;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.log4j.Logger;

import com.aia.prm.BeanFactory;
import com.cfh.domain.UserExample;
import com.cfh.persistence.dao.UserDAO;
import com.cfh.util.Tools;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class IndexAction extends ActionSupport {
	private static final Logger logger = Logger.getLogger(IndexAction.class);

	private String message = "undefined";
	private String status = "出现未知错误";

	public String execute() {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}
		StringBuffer sb = new StringBuffer();
		try {
			UserDAO userDAO = (UserDAO) BeanFactory.get().getBean("userDAO");
			UserExample example = new UserExample();
			int i = userDAO.countByExample(example);
			status = "正常";
			sb.append("成功连接到数据库，用户记录条数: " + i);
		} catch (Throwable e) {
			e.printStackTrace();
			logger.error("web1项目数据库连接异常", e);
			status = "数据库连接异常: " + e.toString();
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw));
			sb.append("\r\n出现错误:" + sw.toString());
		} finally {
			message = sb.toString();
		}
		return Tools.smartReturn(this);
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

}
